package com.sugar.follow.up.module.business.controller.admin.patient.sqpg;

import com.sugar.follow.up.framework.apilog.core.annotation.ApiAccessLog;
import com.sugar.follow.up.framework.common.pojo.CommonResult;
import com.sugar.follow.up.framework.common.pojo.PageParam;
import com.sugar.follow.up.framework.common.pojo.PageResult;
import com.sugar.follow.up.framework.common.util.object.BeanUtils;
import com.sugar.follow.up.framework.excel.core.util.ExcelUtils;
import com.sugar.follow.up.framework.security.core.util.SecurityFrameworkUtils;
import com.sugar.follow.up.module.business.controller.admin.patient.sqpg.vo.PatientSqpgPageReqVO;
import com.sugar.follow.up.module.business.controller.admin.patient.sqpg.vo.PatientSqpgRespVO;
import com.sugar.follow.up.module.business.controller.admin.patient.sqpg.vo.PatientSqpgSaveReqVO;
import com.sugar.follow.up.module.business.dal.dataobject.patientsqpg.PatientSqpgDO;
import com.sugar.follow.up.module.business.service.patientsqpg.PatientSqpgService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static com.sugar.follow.up.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static com.sugar.follow.up.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 术前评估")
@RestController
@RequestMapping("/business/patient-sqpg")
@Validated
public class PatientSqpgController {

    @Resource
    private PatientSqpgService patientSqpgService;

    @PostMapping("/create")
    @Operation(summary = "创建术前评估")
    //@PreAuthorize("@ss.hasPermission('business:patient-sqpg:create')")
    public CommonResult<Long> createPatientSqpg(@Valid @RequestBody PatientSqpgSaveReqVO createReqVO) {
        return success(patientSqpgService.createPatientSqpg(createReqVO, SecurityFrameworkUtils.getLoginUserId()));
    }

    @PutMapping("/update")
    @Operation(summary = "更新术前评估")
    //@PreAuthorize("@ss.hasPermission('business:patient-sqpg:update')")
    public CommonResult<Boolean> updatePatientSqpg(@Valid @RequestBody PatientSqpgSaveReqVO updateReqVO) {
        patientSqpgService.updatePatientSqpg(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除术前评估")
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('business:patient-sqpg:delete')")
    public CommonResult<Boolean> deletePatientSqpg(@RequestParam("id") Long id) {
        patientSqpgService.deletePatientSqpg(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除术前评估")
    //@PreAuthorize("@ss.hasPermission('business:patient-sqpg:delete')")
    public CommonResult<Boolean> deletePatientSqpgList(@RequestParam("ids") List<Long> ids) {
        patientSqpgService.deletePatientSqpgListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得术前评估")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('business:patient-sqpg:query')")
    public CommonResult<PatientSqpgRespVO> getPatientSqpg(@RequestParam("id") Long id) {
        PatientSqpgDO patientSqpg = patientSqpgService.getPatientSqpg(id);
        return success(BeanUtils.toBean(patientSqpg, PatientSqpgRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得术前评估分页")
    //@PreAuthorize("@ss.hasPermission('business:patient-sqpg:query')")
    public CommonResult<PageResult<PatientSqpgRespVO>> getPatientSqpgPage(@Valid PatientSqpgPageReqVO pageReqVO) {
        PageResult<PatientSqpgDO> pageResult = patientSqpgService.getPatientSqpgPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PatientSqpgRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出术前评估 Excel")
    //@PreAuthorize("@ss.hasPermission('business:patient-sqpg:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPatientSqpgExcel(@Valid PatientSqpgPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PatientSqpgDO> list = patientSqpgService.getPatientSqpgPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "术前评估.xls", "数据", PatientSqpgRespVO.class,
                        BeanUtils.toBean(list, PatientSqpgRespVO.class));
    }

}